package com.ruicar.afs.cloud.afscase.loanreviewrule.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruicar.afs.cloud.afscase.loanreviewrule.entity.LoanReviewRule;
import com.ruicar.afs.cloud.afscase.loanreviewrule.mapper.LoanReviewRuleMapper;
import com.ruicar.afs.cloud.afscase.loanreviewrule.service.LoanReviewRuleService;
import com.ruicar.afs.cloud.bizcommon.rules.entity.AfsRuleInfo;
import com.ruicar.afs.cloud.bizcommon.rules.service.AfsRuleInfoService;
import com.ruicar.afs.cloud.common.modules.afscorebusiness.enums.WhetherEnum;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PathVariable;

import java.util.Date;

/**
 * @author: yk.Li
 * @date 2020-06-17 03:57:05
 * @description 复核规则信息表  service实现类
 */
@Service
@AllArgsConstructor
@Data
@Slf4j
public class LoanReviewRuleServiceImpl extends ServiceImpl<LoanReviewRuleMapper, LoanReviewRule> implements LoanReviewRuleService {
    private AfsRuleInfoService afsRuleInfoService;

    /**
     * @param
     * @description 通过业务规则id批量失效规则
     * @author yk.Li
     * @date 2020/7/6
     */
    @Override
    public void deActiveRuleByRuleNo(String[] ids) {
        //使规则失效
        for (String id : ids) {
            LoanReviewRule loanReviewRule = this.getById(id);
            if (null != loanReviewRule) {
                AfsRuleInfo afsRuleInfo = afsRuleInfoService.getById(loanReviewRule.getRuleId());
                if (null != afsRuleInfo) {
                    afsRuleInfoService.deActiveRule(afsRuleInfo.getId());//规则失效
                    log.info("--------------------规则【" + afsRuleInfo.getId() + "】已失效----------------->");
                }
            }
        }
    }

    /**
     * @param
     * @description 停用规则
     * @author yk.Li
     * @date 2020/7/6
     */
    public void deActiveRule(String id) {
        //设为停用
        LoanReviewRule loanReviewRule = this.getById(id);
        if (null != loanReviewRule) {
            loanReviewRule.setIsEnable(WhetherEnum.NO.getCode());
            loanReviewRule.setFailureTime(new Date());
            this.updateById(loanReviewRule);
            //设置规则失效
            AfsRuleInfo afsRuleInfo = afsRuleInfoService.getById(loanReviewRule.getRuleId());
            if (null != afsRuleInfo) {
                afsRuleInfoService.deActiveRule(afsRuleInfo.getId());//规则失效
            }
        }
    }

    /**
     * @param
     * @description 启用规则
     * @author yk.Li
     * @date 2020/7/6
     */
    public void activeRule(@PathVariable String id) {
        //设为启用
        LoanReviewRule loanReviewRule = this.getById(id);
        if (null != loanReviewRule) {
            loanReviewRule.setIsEnable(WhetherEnum.YES.getCode());
            loanReviewRule.setEffectTime(new Date());
            this.updateById(loanReviewRule);
            //激活规则
            AfsRuleInfo afsRuleInfo = afsRuleInfoService.getById(loanReviewRule.getRuleId());
            if (null != afsRuleInfo) {
                afsRuleInfoService.activeRule(afsRuleInfo.getId());//规则生效
            }
        }
    }

}
